package com.cw1.design1Single;

/**
 * @Author qiuMi
 * @description 饿汉式(静态代码块)
 * @Date 2022-09-26 21:39
 **/
public class SingleTest02 {
    public static void main(String[] args) {
        Single2 instance1 = Single2.getInstance();
        Single2 instance2 = Single2.getInstance();
    }
}

/**
 * 饿汉式 -- 静态代码块
 */
class Single2{
    private static Single2 instance;

    // 在静态代码块中执行
    static {
        instance = new Single2();
    }

    /**
     * 无参构造器
     */
    private Single2(){

    }

    public static Single2 getInstance(){
        return instance;
    }
}

/**
 * 饿汉式 - 静态代码块(qiuMI)
 */
class Single22{
    private static Single22 instance;

    static {
        instance = new Single22();
    }
    /**
     *
     */
    private Single22(){}

    private static Single22 getInstance(){
        return instance;
    }
}
